/*********************\
* Author:Dzlua
* QQ:505544956
* Time:2017/03/14
\**********************/
#ifndef __SD_ENGINE_H__
#define __SD_ENGINE_H__
#pragma once

#include <vector>

#include "SDIScript.h"

namespace DuiLib {

#define SDPtr (SDEngine::Interface())

class UILIB_API SDEngine
{
public:
	static SDEngine* Interface();
    // hInst:0,current
    bool Init(HINSTANCE hInst = NULL);
    void Quit();

    //0,remove script.;
    void SetStript(SDIScript* script, bool bAutoDelete = true);
    SDIScript* GetStript();

    void SetError(SDTString err);
    SDTString GetError();
public:
    SDWindow* AddWindow(SDWindow* wnd, bool bAutoDelete = true);
    void RemoveWindow(SDWindow* wnd);
    void RemoveAllWindow();
    int WindowsCount();
    SDWindow* WindowAt(int idx);
    SDWindow* FindWindow(LPCTSTR pstrName);
    SDWindow* FindWindow(HWND hWnd);
    SDWindow* FindWindow(CPaintManagerUI* pm);
public:
    HINSTANCE GetInstance();
    CDuiString GetInstancePath();
    CDuiString GetCurrentPath();
    HINSTANCE GetResourceDll();
    const CDuiString& GetResourcePath();
    const CDuiString& GetResourceZip();
    const CDuiString& GetResourceZipPwd();
    bool IsCachedResourceZip();
    HANDLE GetResourceZipHandle();
    void SetInstance(HINSTANCE hInst);
    void SetCurrentPath(LPCTSTR pStrPath);
    void SetResourceDll(HINSTANCE hInst);
    void SetResourcePath(LPCTSTR pStrPath);
    void SetResourceZip(LPVOID pVoid, unsigned int len, LPCTSTR password = NULL);
    void SetResourceZip(LPCTSTR pstrZip, bool bCachedResourceZip = false, LPCTSTR password = NULL);
    void SetResourceType(int nType);
    int GetResourceType();
    bool GetHSL(short* H, short* S, short* L);
    void SetHSL(bool bUseHSL, short H, short S, short L); // H:0~360, S:0~200, L:0~200 
    void ReloadSkin();
    CPaintManagerUI* GetPaintManager(LPCTSTR pstrName);
    CStdPtrArray* GetPaintManagers();
    bool LoadPlugin(LPCTSTR pstrModuleName);
    CStdPtrArray* GetPlugins();

    void MessageLoop();
    bool TranslateMessage(const LPMSG pMsg);
    void Term();

    void ReloadSharedImages();
    void SetAllDPI(int iDPI);
protected:
    SDEngine();
    ~SDEngine();
private:
	static SDEngine *m_ptr;
    typedef std::vector<SDWindow*> SDWndMap;
    SDWndMap* m_wnds;

    SDIScript* m_script;
    bool m_bAutoDeleteScript;

    SDTString m_strErr;
};

}
#endif //__SD_ENGINE_H__
